package com.yc.shopping.ui;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.yc.shopping.dao.GoodTypeDao;
import com.yc.shopping.util.ShoppingUtil;
import org.eclipse.swt.widgets.Table;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class Dialog_GoodType extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table table;
	private Text text_name;
	
	//创建GoodTypeDao 对象 调用里面的函数
	GoodTypeDao dao = new GoodTypeDao();

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public Dialog_GoodType(Shell parent, int style) {
		super(parent, style);
		setText("商品类型管理");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(430, 497);
		shell.setText(getText());
		//对话框居中
		ShoppingUtil.centerWindows(shell);
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(37, 10, 337, 263);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(100);
		tblclmnNewColumn.setText("编号");
		
		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(100);
		tblclmnNewColumn_1.setText("类型名称");
		
		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_2.setWidth(100);
		tblclmnNewColumn_2.setText("状态");
		
		TableItem tableItem = new TableItem(table, SWT.NONE);
		tableItem.setText(new String[] {"1001", "肉类", "上架"});
		
		Group group = new Group(shell, SWT.NONE);
		group.setText("添加商品类型");
		group.setBounds(37, 297, 337, 145);
		
		Label label = new Label(group, SWT.NONE);
		label.setBounds(10, 41, 90, 24);
		label.setText("类型名称:");
		
		text_name = new Text(group, SWT.BORDER);
		text_name.setBounds(92, 35, 201, 30);
		
		Button btn_add = new Button(group, SWT.NONE);
		//商品类型的添加
		btn_add.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				//从页面获取类型名称
				String tname = text_name.getText().trim();
				try {
					if(text_name.getText().trim().equals("")) {
						ShoppingUtil.showMessage(shell, "错误提示", "类型不能为空");
					}
					int result = dao.add(tname);
					if(result <= 0) {
						ShoppingUtil.showMessage(shell, "错误提示", "类型添加失败");
						return ;
					}
					//清除文本框数据
					text_name.setText("");
					//刷新表格
					showTypes();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		});
		btn_add.setBounds(125, 101, 114, 34);
		btn_add.setText("添加");

		//当所有的组件创建完成后，加载数据
		showTypes();
		
		
		
		
		
	}
	/*
	 * 在表格中显示所有的商品类信息
	 */
	private void showTypes() {
		//清除表格中原有的数据
		table.removeAll();
		
		//查询所有类型
		try {
			List<Map<String,Object>> list = dao.findAll();
			//判断是否有类型信息
			if(list == null || list.isEmpty()) {
				ShoppingUtil.showMessage(shell, "消息提示", "暂无商品类型数据!");
				return;
			}
			//一条数据就是表格中一行
			TableItem item = null;
			for(Map<String,Object> m:list) {
				String state = "上架";
				if("2".equals(ShoppingUtil.objToString(m.get("TSTATUS")))) {
					state = "下架";
				}
				
				
				item = new TableItem(table,SWT.NONE);
				item.setText(new String[]{
						ShoppingUtil.objToString(m.get("TID")),
						ShoppingUtil.objToString(m.get("TNAME")),
						state
				});
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
	}
}
